package com.banmao.desktoptools.util;

import org.apache.commons.lang3.StringUtils;

import java.net.URL;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;

public class ResourceUtil {

    public static URL getResource(ClassLoader classLoader, String path) {
        String decodePath = URLDecoder.decode(path, StandardCharsets.UTF_8);
        if(StringUtils.startsWith(decodePath, "/") || StringUtils.startsWith(decodePath, "\\")) {
            // 前面带有 / ，应用class.getResource
            return ResourceUtil.class.getResource(decodePath);
        } else {
            return classLoader.getResource(decodePath);
        }
    }

}
